use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Config {
    pub host: String,
    pub port: u16,
    #[serde(default)]
    pub username: String,
    pub password: String,
}

pub async fn open(config: &Config) -> Result<redis::aio::MultiplexedConnection, anyhow::Error> {
    let u = format!(
        "redis://{}:{}@{}:{}",
        config.username, config.password, config.host, config.port
    );

    let client = redis::Client::open(u).map_err(anyhow::Error::new)?;
    let conn = client
        .get_multiplexed_async_connection()
        .await
        .map_err(anyhow::Error::new)?;

    Ok(conn)
}
